// pages/mine/myIncome/myIncome.js
const app = getApp(),
	globalData = app.globalData,
	api = require("../../../api/index"),
	utils = require('../../../utils/util');
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		userInfo: {},
		status: 0,
		toast: {
			visible: false,
			text: ''
		},
		imageBaseUri: globalData.imageBaseUri
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		this.reload()
		console.log(this.data.userInfo, 'userInfo')

	},

	reload() {
		this.setData({
			userInfo: wx.getStorageSync('userinfo') || {}
		})
		this.getDetail()
		this.getBenefits()
		this.getBalance()

        console.log(this.data.userInfo, 'userInfo')
        if (this.data.userInfo.AccessToken) {
            this.getUser();
            this.getMySales();
        }
	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {
		this.getBalance()
	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function () {
		let userId = globalData.userInfo.UserId || '';
		console.log(`/pages/index/index?userId=${userId}`, '首页分享链接')
		return {
			title: 'TNTB超多好货等你来看',
			path: `/pages/index/index?userId=${userId}`,
			imageUrl: 'https://xhbehappy.cn/upload/2020082617112274177259.jpg'
		}
	},

	toRank() {
		wx.navigateTo({
			url: '/pages/mine/rank/rank',
		})
	},

	toIncomDetail() {
		wx.navigateTo({
			url: '/pages/mine/myIncome/detail/detail',
		})
	},

	toSpread() {
		wx.navigateTo({
			url: '/pages/mine/spread/spread',
		})
	},

	toGuide() {
		wx.navigateTo({
			url: '/pages/mine/guide/guide',
		})
	},

	toMember(){
		wx.navigateTo({
		  url: '/pages/mine/members/members',
		})
	},

	toLevel(){
		wx.navigateTo({
		  url: '/pages/mine/myLevel/myLevel',
		})
	},

	// 获取收益详情
	async getDetail() {
		try {
			let res = await api.balance.getBenefitsDetail(),
				data = res.data.ResData || {};
			console.log(res, '收益详情')
			this.setData({
				status: 2,
				pageData: data
			})
		} catch (e) {
			console.log(e, '获取收益详情失败')
		}
	},

	// 获取我的收益
	async getBenefits() {
		try {
			let res = await api.balance.getMyBenefits(),
				data = res.data.ResData || {};
			this.setData({
				status: 2,
				total: data
			})
			console.log( this.data.total, '我的收益')
		} catch (e) {
			console.log(e, '获取我的收益失败')
		}
	},

	// 获取我的可提现金额
	async getBalance() {
		try {
			let res = await api.balance.getMyBalance(),
				data = res.data.ResData || {};
			this.setData({
				status: 2,
				balance: data
			})
			console.log(res, '可提现金额')
		} catch (e) {
			console.log(e, '获取可提现金额失败')
		}
	},

	// 提现
	withdrawl() {
		wx.navigateTo({
		  url: '/pages/mine/withdrawal/withdrawal',
		})
		// this.setData({
		// 	toast: {
		// 		visible: true,
		// 		text: '功能暂未开放，敬请期待'
		// 	}
		// })
	},

	// 登录后获取用户等级信息
    async getUser() {
        try {
            let res = await api.user.getUser(),
                data = res.data.ResData || {};
            this.setData({
                agentInfo: data,
                status: 2
            })
            console.log(res, '获取用户等级信息')
        } catch (e) {
            console.log(e, '获取用户等级信息失败')
            this.setData({
                toast: {
                    visible: true,
                    text: e.data.ErrMsg
                },
                status: 1
            })
        }
    },

    // 获取本人的销售额
    async getMySales() {
        try {
            let res = await api.member.getSales();
            console.log(res, '本人销售额')
            this.setData({
                userData: res.data.ResData || {}
            })
        } catch (e) {
            console.log(e, '获取本人销售额失败')
        }
    },

})